package com.bizdk.fastfx.controller;

import com.bizdk.fastfx.Context;
import com.bizdk.fastfx.fxsupport.FxmlView;
import com.bizdk.fastfx.fxsupport.JavaFxController;
import com.bizdk.fastfx.model.DoctorModel;
import com.bizdk.fastfx.model.PatientModel;
import com.bizdk.fastfx.service.DoctorService;
import com.bizdk.fastfx.service.PatientService;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.*;

@Controller
@FxmlView("/view/MainStage.fxml")
public class MainStageController extends JavaFxController {
    @FXML
    private TabPane tp_main;
    @FXML
    private Tab tab_doctor;
    @FXML
    private Tab tab_patient;

    @FXML
    private TableView<PatientModel> tv_patient;

    @FXML
    private TableView<DoctorModel> tv_doctor;

    @FXML
    private TableColumn<DoctorModel, String> tc_doctor_id;

    @FXML
    private TableColumn<DoctorModel, String> tc_doctor_name;

    @FXML
    private TableColumn<DoctorModel, String> tc_doctor_title;

    @FXML
    private TableColumn<PatientModel, String> tc_patient_id;

    @FXML
    private TableColumn<PatientModel, String> tc_patient_name;

    @FXML
    private TableColumn<PatientModel, String> tc_patient_gender;

    @Autowired
    private ImageViewerStageController imageViewerStageController;

    @Autowired
    private MainStageController mainStageController;

    @Autowired
    private AddDoctorStageController doctorStageController;

    @Autowired
    private AddPatientStageController patientStageController;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientService patientService;

    public void initialize(URL location, ResourceBundle resources) {
        // 设置表格字段的属性和模板数据的对应关系
        tc_doctor_id.setCellValueFactory(new PropertyValueFactory<>("id"));
        tc_doctor_name.setCellValueFactory(new PropertyValueFactory<>("name"));
        tc_doctor_title.setCellValueFactory(new PropertyValueFactory<>("title"));

        tc_patient_id.setCellValueFactory(new PropertyValueFactory<>("id"));
        tc_patient_name.setCellValueFactory(new PropertyValueFactory<>("name"));
        tc_patient_gender.setCellValueFactory(new PropertyValueFactory<>("gender"));

        // table row 点击事件
        tv_patient.setRowFactory(tv -> {
            TableRow<PatientModel> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                // 点击两次 且 row不为空
                if (event.getClickCount() == 2 && (!row.isEmpty())) {
                    PatientModel itemInfo = row.getItem();
                    imageViewerStageController.show(itemInfo);
                }
            });
            return row;
        });
    }

    public void getPartientList() {
        tv_patient.setItems(patientService.listModel());
    }

    public void getDoctorList() {
        tv_doctor.setItems(doctorService.listModel());
    }

    public void handlerDocterBtnAdd(ActionEvent event) {
        event.consume();
        DoctorModel doctor = new DoctorModel();
        doctor.setCreate(true);
        doctorStageController.show(doctor);
    }

    public void handlerDocterBtnUpdate(ActionEvent event) {
        event.consume();
        DoctorModel selectedDoctor = tv_doctor.getSelectionModel().getSelectedItem();
        if (selectedDoctor != null) {
            doctorStageController.show(selectedDoctor);
        }
    }

    public void handlerDocterBtnDel(ActionEvent event) {
        event.consume();
        DoctorModel selectedDoctor = tv_doctor.getSelectionModel().getSelectedItem();
        if (selectedDoctor != null) {
            doctorService.delModel(selectedDoctor);
            getDoctorList();
        }
    }


    public void handlerPatientBtnAdd(ActionEvent event) {
        event.consume();
        PatientModel patient = new PatientModel();
        patientStageController.show(patient);
    }

    public void handlerPatientBtnUpdate(ActionEvent event) {
        event.consume();
        PatientModel selectedPatient = tv_patient.getSelectionModel().getSelectedItem();
        if (selectedPatient != null) {
            patientStageController.show(selectedPatient);
        }
    }

    public void handlerPatientBtnDel(ActionEvent event) {
        event.consume();
        PatientModel selectedPatient = tv_patient.getSelectionModel().getSelectedItem();
        if (selectedPatient != null) {
            patientService.delModel(selectedPatient);
            getPartientList();
        }
    }

    @Override
    public void show() {
        super.show();
        if (Context.isAdmin) {
            getPartientList();
            getDoctorList();
        } else {
            getPartientList();
            tab_patient.getTabPane().getTabs().remove(tab_doctor);
        }
    }
}